package org.web.base.helper;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.commons.lang3.StringUtils;
import org.web.base.domain.exception.ResultMessageEnum;
import org.web.base.domain.exception.ServiceException;
import org.web.base.domain.helper.ServiceExceptionHelper;
import org.web.base.helper.filter.PropertiesParamType;

public class PropertiesHelper {

    private static Object getValue(ResourceBundle resourceBundle, String key, Object defaultValue, PropertiesParamType propertiesParamType) throws ServiceException {
        Object val = defaultValue;
        try {
            if (resourceBundle.containsKey(key)) {
                String value = resourceBundle.getString(key);
                if (StringUtils.isNotBlank(value)) {
                    switch (propertiesParamType) {
                        case String:
                            val = value;
                            break;
                        case Boolean:
                            val = Boolean.valueOf(value.trim());
                            break;
                        case Integer:
                            val = Integer.valueOf(value.trim());
                            break;
                        case Long:
                            val = Long.valueOf(value.trim());
                            break;
                        default:
                            throw ServiceExceptionHelper.buildServiceException(ResultMessageEnum.INIT_PARAM_INVALID, ResultMessageEnum.INIT_PARAM_INVALID.getMessage() + ", 参数名称");
                    }
                }
            }
        } catch (Exception e) {
            throw ServiceExceptionHelper.buildServiceException(ResultMessageEnum.INIT_PARAM_INVALID, ResultMessageEnum.INIT_PARAM_INVALID.getMessage() + ", 参数名称");
        }
        return val;
    }

    public static String getString(ResourceBundle resourceBundle, String key, Object defaultValue) throws ServiceException {
        return (String) getValue(resourceBundle, key, defaultValue, PropertiesParamType.String);
    }

    public static Long getLong(ResourceBundle resourceBundle, String key, Object defaultValue) throws ServiceException {
        return (Long) getValue(resourceBundle, key, defaultValue, PropertiesParamType.Long);
    }

    public static Integer getInteger(ResourceBundle resourceBundle, String key, Object defaultValue) throws ServiceException {
        return (Integer) getValue(resourceBundle, key, defaultValue, PropertiesParamType.Integer);
    }

    public static Boolean getBooelan(ResourceBundle resourceBundle, String key, Object defaultValue) throws ServiceException {
        return (Boolean) getValue(resourceBundle, key, defaultValue, PropertiesParamType.Boolean);
    }

    public static ResourceBundle getResourceBundle(String bundleName, String baseName) throws MissingResourceException {
        Locale locale;
        if (StringUtils.isEmpty(baseName)) {
            locale = Locale.getDefault();
        } else {
            locale = new Locale(baseName);
        }
        return ResourceBundle.getBundle(bundleName, locale);
    }


    /**
     * 读取指定路径下的资源文件
     *
     * @param bundleName properties文件路径
     * @param key        配置文件中的KEY
     * @return value
     */
    public static String getString(String bundleName, String key) throws MissingResourceException {
        return getResourceBundle(bundleName, null).getString(key);
    }

}
